#pragma  once


#include <json.hpp>
#include <singleton.hpp>
#include "net/pinger.hpp"
#include <unordered_map>
#include <memory>
#include <set>
#include "device.hpp"
#include <shared_mutex>

namespace bz
{
	class device_state_observer {
	public:
		virtual void  on_state_change() = 0;
	};


	class device_state 
	{
	public:
		sigletonclass(device_state);

		void reg_obs(device_state_observer* ob);
		void unreg_obs(device_state_observer* ob);

		device* get_device(int id);
			

		void update_device();

	private:

		
		device_state();

		~device_state();

		bool check_state();
		void start_check();


		asio::io_service ios_;
		boost::asio::io_service::work work_;
		std::weak_ptr<boost::asio::steady_timer> check_timer_;
		std::unordered_map<string, std::shared_ptr<pinger>> ping_map_;
		std::set<device_state_observer*> obs_list_;
		std::thread thread_;
		std::unordered_map<int, device> device_map_;
		std::vector<device> device_list_;
		std::shared_mutex  mutex_;

		bool has_change_;
		//std::vector<std::string> fsbc_online_vec_;
	};
}